#include "makeDir.h"
char* makeDir(char *path,char* directory)		
{
	if(mh==NULL)
		return ERR_VFS_MAKEDIR_05;
	if(path[0]!='/')
		return ERR_VFS_MAKEDIR_02;
	//traversenAryTree(nAryRoot);
	int z=strlen(path);
	if(z!=1 && path[z-1]=='/')
		path[z-1]='\0';
	//printf("%s\n",path);
	int flag = 1;
	int check=0;
	int currentFileDescriptors = mh->countOfFileDescriptors;
	int maxFileDescriptors = mh->maxDescriptors;
	int noOfBlocks = maxFileDescriptors/2;
	int iterDescriptors=0;
	char *tempPath=(char*)malloc(sizeof(char)*(strlen(path)+1));;
	char *dirName=(char*)malloc(sizeof(char)*(strlen(directory)+1));
	int alreadyThere=0;
	int newNodeCreated=0;
	strcpy(dirName, directory);
	flag = validDirnameCheck(dirName);
	if( flag == 0)
		return ERR_VFS_MAKEDIR_02;
	else
	{
		if(strcmp(path,"/")!=0)
			strcat(path,"/");
		strcat(path,directory);
		//printf("%s\n",path);
		char *dirpath=(char*)malloc(strlen(path)+1);
		strcpy(dirpath,path);
		char *token[20];
		int i=0,j=0,k=0;
		token[i]=strtok(dirpath,"/");
		while(token[i]!=NULL)
		{
			i++;
			token[i]=strtok(NULL,"/");
		}
		i=0;
		nAryNode *temp = nAryRoot, *newNode, *lsibling;
		newNode = (nAryNode*) malloc (sizeof(nAryNode));
		
		if((token[i]!=NULL)&&(nAryRoot==NULL))
		{
			return ERR_VFS_MAKEDIR_01;
		}
		else
		{
			if(temp!= NULL)
			{
				while((token[i]!=NULL) && strcmp(token[i],temp->name)!=0)
				{
					alreadyThere=0;
					newNode->name=(char*)malloc(sizeof(char)*(strlen(token[i])+1));
					if(temp->child == NULL)
					{
						strcpy(newNode->name,token[i]);
						newNode->child = NULL;
						newNode->sibling = NULL;
						temp->child = newNode;
						temp = temp->child;
						newNodeCreated=1;
					}
					else
					{
						temp=temp->child;
						while(token[i]!=NULL && strcmp(temp->name,token[i])==0)
						{
							if(token[i+1]==NULL && strcmp(token[i],temp->name)==0)
							{
								descriptors *des = (descriptors*)searchBST(path);
								if (des == NULL)
								{
									return ERR_VFS_MAKEDIR_03 ;
								}
								else
								{
									while(temp->sibling != NULL)
									{
										if(strcmp(token[i],temp->sibling->name)!=0)
										{	
											temp = temp->sibling;
										}
										else
											return ERR_VFS_MAKEDIR_03 ;
									}
								}
							}
							if(strcmp(temp->name,token[i])==0)
								alreadyThere=1;
							if(strcmp(temp->name,token[i])==0 && token[i+1]!=NULL)
								alreadyThere=0;
							if(temp->child==NULL)
							{
								check=1;
								alreadyThere=0;
								i++;
								break;
							}
							temp=temp->child;
							i++;
						}
						while(temp->sibling!=NULL)
						{
							if(token[i]==NULL)
							{
								descriptors *des = (descriptors*)searchBST(path);
								if (des == NULL)
								{
									return ERR_VFS_MAKEDIR_03 ;
								}
								else
								{
									while(temp->sibling != NULL)
									{
										if(strcmp(token[i],temp->sibling->name)!=0)
										{	
											temp = temp->sibling;
										}
										else
											return ERR_VFS_MAKEDIR_03 ;
									}
								}
							}
							else
							{
								temp=temp->sibling;
								if(strcmp(temp->name,token[i])==0)
									alreadyThere=1;
							}
							if(alreadyThere)
								break;
						}
						if(!alreadyThere)
						{
							sprintf(newNode->name,"%s",token[i]);
							newNode->child = NULL;
							newNode->sibling = NULL;
							if(check)
								temp->child=newNode;
							else
								temp->sibling = newNode;
							temp = newNode;
							newNodeCreated=1;
						}
					}
					//printf("%d %d\n",alreadyThere,newNodeCreated);
					//printf("%s %s\n",temp->name,token[i]);
								
					if(newNodeCreated)
					{
						descriptors *d = (descriptors*) malloc(sizeof(descriptors));
						strcpy(d->fileName,token[i]);
						int temp=i;
						int tempIter=0;
						while(path[tempIter]!='\0')
						{
							if(path[tempIter]=='/')
								temp--;
							if(temp==-1)
								break;
							tempIter++;
						}
						int iTemp=0;
						while(iTemp<tempIter)
						{
							tempPath[iTemp]=path[iTemp];
							iTemp++;
						}
						if(iTemp==0)
						{
							tempPath[0]='/';
							iTemp++;
						}
						tempPath[iTemp]='\0';
						strcpy(d->location,tempPath);
						d->type = 'd';
						d->size = 0;
						d->blockNumber = -1;
						iterDescriptors=0;
						while(iterDescriptors<maxFileDescriptors)
						{
							if(h->desc[iterDescriptors].type=='f' || h->desc[iterDescriptors].type=='d')
								iterDescriptors++;
							else
								break;
						}
						h->desc[iterDescriptors] = *d;
						mh->countOfFileDescriptors=mh->countOfFileDescriptors+1;
						int hashTableRet=hashTableMethod(h->desc,maxFileDescriptors);
						if(hashTableRet!=0)
							return "FAILURE <while creating hashTable>";
					}
					i++;
				}
			}
			int nAryRet=createnAryTree(maxFileDescriptors,h->desc);
			if(nAryRet!=0)
				return "FAILURE <while creating nAryTree>";
		}
	}
	//printf("%s\n",nAryRoot->name);
	return "SUCCESS";
}

	
int validDirnameCheck(char dirName[])
{
    int i = 0 ;
    for(i=0;dirName[i] != '\0';i++)
    {
        if (dirName[i]=='/')
			return FALSE;
    }
    return TRUE;

}
